package com.mitlab.dev.draw.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mitlab.dev.draw.control.ClazzController;

/**
 * Servlet implementation class DrawServlet
 */
public class DrawServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectMapper om = new ObjectMapper();
		if (request.getRequestURI().endsWith("flowModel.do")) {
			om.writeValue(response.getOutputStream(), om.readValue(com.mitlab.dev.draw.mapper.GraphLinksModel.class.getResourceAsStream("flow.json"), com.mitlab.dev.draw.mapper.GraphLinksModel.class));
			System.out.println("flowModel.do---");
			return;
		}else if (request.getRequestURI().endsWith("mapperModel.do")) {
			String phaseId = request.getParameter("phaseId");
			String json = phaseId == null || "".equals(phaseId) ? "empty.json" : "mapper.json";
			System.out.println("mapperModel.do---");
			om.writeValue(System.out, com.mitlab.dev.draw.mapper.GraphLinksModel.class);
			om.writeValue(response.getOutputStream(), om.readValue(com.mitlab.dev.draw.mapper.GraphLinksModel.class.getResourceAsStream(json), com.mitlab.dev.draw.mapper.GraphLinksModel.class));
			return;
		}else if (request.getRequestURI().endsWith("getAllClass.do")) {
			System.out.println("getAllClass.do---");
			ClazzController clazzController=new ClazzController();
		om.writeValue(response.getOutputStream(),clazzController.getAllClass());
			return;
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectMapper om = new ObjectMapper();
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		if (request.getRequestURI().endsWith("linksModel.do")) {
			com.mitlab.dev.draw.mapper.GraphLinksModel glm = om.readValue(request.getParameter("gojs"), com.mitlab.dev.draw.mapper.GraphLinksModel.class);
			response.getOutputStream().print(glm.process());
			System.out.println(glm.process());
			return;
		}
		if (request.getRequestURI().endsWith("generateCode.do")) {
			com.mitlab.dev.draw.mapper.GraphLinksModel glm = om.readValue(request.getParameter("graph"), com.mitlab.dev.draw.mapper.GraphLinksModel.class);
			response.getOutputStream().print(glm.process());
			System.out.println(glm.process());
			return;
		}
	}

}
